{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3acf2a05-404c-4982-82d2-2fa723f2f9c8",
   "metadata": {},
   "source": [
    "Chapter 07\n",
    "# 笛卡儿积\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "51b9ff77-deee-4a5c-8b09-0d223dfd973f",
   "metadata": {},
   "outputs": [],
   "source": [
    "column1 = [1, 2, 3, 4]\n",
    "column2 = ['a', 'b', 'c']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "38490dfa-d180-4189-8e11-86a31822f25e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c'), (4, 'a'), (4, 'b'), (4, 'c')]\n"
     ]
    }
   ],
   "source": [
    "cartesian_product = [(x, y) for x in column1 for y in column2]\n",
    "\n",
    "print(cartesian_product)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "67146ad9-ac03-413a-9bd5-249a33f894a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 'a'), (2, 'a'), (3, 'a'), (4, 'a')]\n",
      "[(1, 'b'), (2, 'b'), (3, 'b'), (4, 'b')]\n",
      "[(1, 'c'), (2, 'c'), (3, 'c'), (4, 'c')]\n"
     ]
    }
   ],
   "source": [
    "cartesian_product = [[(x, y) for x in column1] for y in column2]\n",
    "\n",
    "for prod_idx in cartesian_product:\n",
    "    print(prod_idx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "77e12aa2-9adb-45e9-a5b5-380eb1422d28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c'), (4, 'a'), (4, 'b'), (4, 'c')]\n"
     ]
    }
   ],
   "source": [
    "from itertools import product\n",
    "\n",
    "cartesian_product = list(product(column1, column2))\n",
    "print(cartesian_product)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
